<?php
/**
 *  用户反馈选项
 */
namespace Clientside\Controller;

use Common\Controller\AdminbaseController;
use Think\Exception;

class AdminSuggestionController extends AdminbaseController {

    protected $advices_model;

    function _initialize() {
        parent::_initialize();
        $this->advices_model = D("UserAdvices");
    }

    /**
     * 意见反馈列表
     * @return [type] [description]
     */
    public function index()
    {

        $where['status']=array(array('neq','-1'));

        $this->advices_model
        ->alias("a")
        ->where($where);

        $count=$this->advices_model->count();

        $page = $this->page($count, 20);


        $this->advices_model
        ->alias("a")
        ->join("__USERS__ c ON a.uid = c.id")
        ->where($where)
        ->limit($page->firstRow , $page->listRows)
        ->order("a.create_time DESC");
        $this->advices_model->field('a.*,c.user_login,c.user_nicename');
        $posts=$this->advices_model->select();


        $this->assign("page", $page->show('Admin'));
        $this->assign("formget",array_merge($_GET,$_POST));
        $this->assign("posts",$posts);

        $this->display();
    }

    /**
     * 投诉建议选项
     * @return [type] [description]
     */
    public function advices()
    {
        $options = D('Suggestions')->select();
        foreach ($options as $key => $vo) {

            $suggestion_id = $vo['id'];

            $user_count = D('UserSuggestions')->where(['suggestion_id'=>$suggestion_id])->Count();

            $options[$key]['choose_count'] = $user_count;
        }

        $this->assign('options',$options);

        $this->display();
    }

    public function add()
    {
        $this->display('add_edit');
    }

    public function edit($id = 0)
    {
        $option = D('Suggestions')->where(['id'=>$id])->find();
        $this->assign('data',$option);
        $this->display('add_edit');

    }

    public function store()
    {
        if(!IS_POST){
            $this->error('非法请求');
        }
        $id = I('id',0);

        $content = I('content');
        $data = compact('id','content');
        $data =  array_filter($data);

        try{
            if($id){
                //编辑
                D('Suggestions')->add($data,null,true);

            } else {
                //添加
                D('Suggestions')->add($data);

            }
            $this->success('保存成功',U('AdminSuggestion/index'));
        }catch (Exception $e){
            $this->error('保存失败');
        }


    }
    public function delete($id = 0)
    {
        $where = null;
        $whereFoo = null;
        if(isset($_GET['id'])){
            $id = I("get.id",0,'intval');
            $where = ['id'=>$id];
            $whereFoo = ['suggestion_id'=>$id];
        }

        if(isset($_POST['ids'])){
            $ids = I('post.ids/a');
            $where = ['id'=>['in',$ids]];
            $whereFoo = ['suggestion_id'=>['in',$ids]];

        }

        try{
            D('Suggestions')->where($where)->delete();
            D('UserSuggestions')->where($whereFoo)->delete();
            $this->success("删除成功！");
        }catch (Exception $e){
            $this->error($e->getMessage());
        }


    }

}